package com.jwdf.uda.ui.fragment.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.app.AppContext;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentSettingBinding;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.LogoutAction;
import com.jwdf.uda.ui.activity.FeedbackActivity;
import com.jwdf.uda.ui.activity.H5Activity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.update.UpdateChecker;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.FileUtils;
import com.jwdf.uda.utils.UnitUtils;
import com.jwdf.uda.view.dialog.ConfirmDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.message.PushAgent;

import retrofit2.Response;

/**
 * 类说明：
 */
public class SettingFragment extends BaseFragment {
    private FragmentSettingBinding mBinding;
    private ConfirmDialog mClearDialog;
    private ConfirmDialog mLogoutDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentSettingBinding.inflate(inflater, container, false);
        mBinding.setSettingFragment(this);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.settingUpgradeTip.setText(getString(R.string.setting_version_latested, AppContext.getAppVersionName(getActivity())));
        reflashCacheView();
    }
    private void reflashCacheView() {
        try {
            long size = FileUtils.getFileOrDirectorySize(ImageLoader.getInstance().getDiskCache().getDirectory());
            mBinding.settingCacheSize.setText(UnitUtils.FormatFileSize(size));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void clickCacheClear(View view) {
        if (mClearDialog == null) {
            mClearDialog = new ConfirmDialog(getActivity(), getString(R.string.dialog_toast_clear), getString(R.string.dialog_cancel), getString(R.string.dialog_sure), new ConfirmDialog.CallbackListener() {

                @Override
                public void clickNo() {

                }

                @Override
                public void clickYes() {
                    ImageLoader.getInstance().getDiskCache().clear();
                    reflashCacheView();
                }
            });
        }
        mClearDialog.show();
    }
    public void clickFeedback(View view) {
        startActivityFromFragment(FeedbackActivity.class);
    }

    public void clickEvaluate(View view) {
        //TODO

    }

    public void clickAbout(View view) {
        String url = ConfigUtils.getAboutPage(view.getContext());
        StringBuilder stringBuilder = new StringBuilder("http://");
        if (url.contains("http")) {
            H5Activity.start(view.getContext(), url);
        } else {
            H5Activity.start(view.getContext(), stringBuilder.append(url).toString());
        }
    }

    public void clickUpgrade(View view) {
        //检查升级
        UpdateChecker.checkForDialog(getActivity());
    }

    public void clickLogout(View view) {
        if (mLogoutDialog == null) {
            mLogoutDialog = new ConfirmDialog(getContext(), getString(R.string.dialog_toast_logout), getString(R.string.dialog_cancel), getString(R.string.dialog_sure), new ConfirmDialog.CallbackListener() {
                @Override
                public void clickNo() {

                }

                @Override
                public void clickYes() {
                    doLogoutAction();
                }
            });
        }
        mLogoutDialog.show();
    }

    private void doLogoutAction() {
        showProgressDialog();
        onNewRequestCall(LogoutAction.newInstance(getContext()).request(new AHttpService.IResCallback<PbBean.PbResUserLogout>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserLogout> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doLogoutSuccess();
                }
            }
        }));
    }

    private void doLogoutSuccess() {
        ConfigUtils.removeCurrentUser(getContext());
        ConfigUtils.setHttpHeadersCookie(getContext(),"");
        PushAgent.getInstance(getContext()).disable();
        UserEvent event = new UserEvent();
        event.setEvent(UserEvent.EVENT_LOGOUT);
        EventBusUtil.post(event);
    }

}
